package com.wbs.shop.nuby.utils;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;


import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.wbs.shop.nuby.App;
import com.wbs.shop.nuby.commom.Key;

public class NubyLocalBroadcastManager {
    public static void sendRefreshPageBroadcastSync(String url) {
        Intent intent = new Intent(Key.ACTION_REFRESH_PAGE);
        intent.putExtra(Key.URL, url);
        LocalBroadcastManager.getInstance(App.getApp()).sendBroadcastSync(intent);
    }

    public static void sendGoIndexBroadcastSync(int index, boolean refresh) {
        Intent intent = new Intent(Key.ACTION_INDEX_CHANG);
        intent.putExtra(Key.KEY_INDEX, index);
        intent.putExtra(Key.KEY_REFRESH, refresh);
        LocalBroadcastManager.getInstance(App.getApp()).sendBroadcastSync(intent);
    }

    public static void sendChangSidBroadcastSync(String sid, String aid) {
        Intent intent = new Intent(Key.ACTION_INDEX_SID_CHANG);
        intent.putExtra(Key.SID, sid);
        intent.putExtra(Key.AID, aid);
        LocalBroadcastManager.getInstance(App.getApp()).sendBroadcastSync(intent);
    }

    public static void sendLoginSuccessBroadcastSync() {
        Intent intent = new Intent(Key.ACTION_LOGIN_SUCCESS);
        LocalBroadcastManager.getInstance(App.getApp()).sendBroadcastSync(intent);
    }

//    public static void sendSuccessBroadcastSync() {
//        Intent intent = new Intent(Key.ACTION_REFRESH_FLAG);
//        LocalBroadcastManager.getInstance(App.getApp()).sendBroadcastSync(intent);
//    }

    //    public static void sendFinishBroadcastSync() {
//        Intent intent = new Intent(Key.ACTION_FINISH);
//        LocalBroadcastManager.getInstance(App.getApp()).sendBroadcastSync(intent);
//    }
    public static void registerEventReceiver(BroadcastReceiver broadcastReceiver) {
        registerReceiver(broadcastReceiver, Key.ACTION_REFRESH_PAGE);
    }

    public static void registerMainReceiver(BroadcastReceiver broadcastReceiver) {
        registerReceiver(broadcastReceiver, Key.ACTION_INDEX_CHANG, Key.ACTION_INDEX_SID_CHANG);
    }

//    public static void registerRefreshReceiver(BroadcastReceiver broadcastReceiver) {
//        registerReceiver(broadcastReceiver, Key.ACTION_REFRESH_FLAG, Key.ACTION_FINISH);
//    }

    public static void registerLoginSuccessReceiver(BroadcastReceiver broadcastReceiver) {
        registerReceiver(broadcastReceiver, Key.ACTION_LOGIN_SUCCESS);
    }

    private static void registerReceiver(BroadcastReceiver broadcastReceiver, String... action) {
        IntentFilter intentFilter = new IntentFilter();
        if (action != null && action.length > 0)
            for (String anAction : action) intentFilter.addAction(anAction);
        LocalBroadcastManager.getInstance(App.getApp()).registerReceiver(broadcastReceiver, intentFilter);
    }

    public static void unregisterReceiver(BroadcastReceiver broadcastReceiver) {
        LocalBroadcastManager.getInstance(App.getApp()).unregisterReceiver(broadcastReceiver);
    }
}
